from tests.rypark_background.get_cookie import *
from common.config import *
import requests


class Role:
    # =====================内部后台=====================
    # 新建角色
    def create_role(self, env,authIds,name):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/role/add"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": internal_cookies(env)}
        dict = {"authIds": authIds,
                "name": name,
                "remark": "自动化生成角色"}
        return requests.post(url, headers=headers, json=dict).json()

    # 编辑角色
    def update_role(self, env, authIds, name, id):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/role/update"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": internal_cookies(env)}
        dict = {"authIds": authIds,
                "name": name,
                "id": id}
        return requests.post(url, headers=headers, json=dict).json()

    # 角色列表
    def role_list(self, env, status, name=None):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/role/list"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": internal_cookies(env)}
        dict = {"status": status, "name": name}
        return requests.post(url, headers=headers, json=dict).json()

    # 角色详情
    def detail_role(self, env, id):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/role/detail"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": internal_cookies(env)}
        return requests.get(url, headers=headers, params={"id": id}).json()

    # 启用/禁用
    # status:0禁用 1启用
    def change_role(self, env, roleId, status):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/role/changeStatus"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": internal_cookies(env)}
        dict = {"roleId": roleId,
                "status": status}
        return requests.post(url, headers=headers, json=dict).json()

    # 查询用户已关联、可关联角色
    def get_user_role(self, env, userId):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/role/getUserRole"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": internal_cookies(env)}
        return requests.get(url, headers=headers, params={"userId": userId}).json()

    # 树型获取所有权限
    def get_auth_tree(self, env):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/role/getAuthTree"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": internal_cookies(env)}
        return requests.get(url, headers=headers).json()

    # 账号关联角色(在account中case)
    def bind_role(self, env, roleId, userId):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/role/bindRole"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": internal_cookies(env)}
        dict = {"roleId": roleId,
                "userId": userId}
        return requests.post(url, headers=headers, json=dict).json()

    # 选中单个角色，查询拥有的权限
    def get_role_power(self, env, roleId):
        base_url = getconf(env, "rypark_url")
        url = base_url + "easypark-backend/role/getRoleAuth"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": internal_cookies(env)}
        return requests.get(url, headers=headers, params={"roleId": roleId}).json()

    # 选中多个角色，查询拥有的权限
    def get_roles_powers(self, env, roleIds):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/role/getRoleAuthTree"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": internal_cookies(env)}
        dict = {"roleIds": roleIds}
        return requests.post(url, headers=headers, json=dict).json()
